package com.homedo.microservice.open.service.gateway.manager.web.cas.filter;

import com.homedo.common.util.Detect;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class HttpParamsFilter implements Filter {
    public static final String REQUESTED_URL = "CasRequestedUrl";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();

        String requestPath = request.getScheme() + "://" + request.getServerName() + ":"
                + request.getServerPort() + request.getRequestURI();

        if (!requestPath.contains("login/cas") && !requestPath.contains("logout")) {
            String queryString = request.getQueryString();
            if (Detect.notEmpty(queryString)) {
                requestPath += "?" + queryString;
            }
            session.setAttribute(REQUESTED_URL, requestPath);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}